﻿Imports System.Text.RegularExpressions

Public Class Preview

    Private _previmg As Image
    Private _layout As String
    Private _toptext As String
    Private _picbox As PictureBox

    Property PictureBox() As PictureBox
        Get
            Return _picbox
        End Get
        Set(value As PictureBox)
            _picbox = value
        End Set
    End Property

    Property PrevImg() As Image
        Get
            Return _previmg
        End Get
        Set(value As Image)
            _previmg = value
        End Set
    End Property

    Property ImgLayout() As String
        Get
            Return _layout
        End Get
        Set(value As String)
            _layout = value
        End Set
    End Property

    Property TopText() As String
        Get
            Return _toptext
        End Get
        Set(value As String)
            _toptext = value
        End Set
    End Property

    Private m_PanStartPoint As New Point

    Public ActivePBs As New List(Of PictureBox)

    Private Sub Preview_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        My.Settings.PrevWinPos = Me.Location
        My.Settings.PrevWinCloseState = Me.WindowState
        My.Settings.PrevWinSize = Me.Size
        My.Settings.PrevWinImgZoom = ZoomCB.Text
    End Sub

    Private Sub Preview_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        Me.Font = My.Settings.UIFont
        '' Me.Size = New Size(My.Settings.PrevW + System.Windows.Forms.SystemInformation.VerticalScrollBarWidth,
        '                   My.Settings.PrevH + Panel1.Height + _
        '                   System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight + _
        '                   +SystemInformation.ToolWindowCaptionHeight)
        Me.Size = My.Settings.PrevWinSize
        Me.Location = My.Settings.PrevWinPos
        Me.WindowState = My.Settings.PrevWinCloseState
        If Not FormOnScreen(Me) Then
            Me.Location = New Point(0, 0)
        End If

        If My.Settings.PrevWinState = True Then
            Me.WindowState = FormWindowState.Maximized
        End If

        Panel2.AutoScroll = True

        Dim selrow() As DataRow = GameSetMatch._MainDT.Select("GID = " & GameSetMatch.TopSelectedRow)
        SeldCB.Checked = selrow(0).Item("Sel")

        If Not GameSetMatch.NewPB Is Nothing Then
            ActivePBs.Add(GameSetMatch.NewPB)
        End If
        If Not GameSetMatch.LocalPB Is Nothing Then
            ActivePBs.Add(GameSetMatch.LocalPB)
        End If
        If Not GameSetMatch.CloudPB Is Nothing Then
            ActivePBs.Add(GameSetMatch.CloudPB)
        End If

        With PrevImgPB
            If _layout = "None" Then
                .Size = New Size(_previmg.Width, _previmg.Height)
                .Dock = DockStyle.None
            Else
                .Size = New Size(My.Settings.PrevW, My.Settings.PrevH)
                .Dock = DockStyle.Fill
            End If

            .Image = _previmg
            .SizeMode = ImageLay(_layout)

        End With

        TopTextLB.Text = _toptext

        'ZoomCB.Text = _layout
        ZoomCB.Text = My.Settings.PrevWinImgZoom
        ZoomCB_SelectedIndexChanged(ZoomCB, Nothing)

        If _picbox.Name = "FuzzyImagePB" Then
            Me.Text = "Search Image Preview"
            SeldCB.Visible = False
            ImgR.Visible = False
            ImgL.Visible = False
        End If

    End Sub

    Sub KeyPressMap(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode = Keys.W Then
            SendKeys.Send("{Up}")
        End If
        If e.KeyCode = Keys.A Then
            ImgL_Click(Nothing, New MouseEventArgs(Windows.Forms.MouseButtons.Left,
                                                   1, MousePosition.X, MousePosition.Y, 120))
        End If
        If e.KeyCode = Keys.S Then
            SendKeys.Send("{Down}")
        End If
        If e.KeyCode = Keys.D Then
            ImgR_Click(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Right,
                                                  1, MousePosition.X, MousePosition.Y, 120))
        End If

        If e.KeyCode = Keys.Q Then
            Me.Close()
        End If

        If e.KeyCode = Keys.E Then
            If SeldCB.Checked = False Then
                SeldCB.Checked = True
            Else
                SeldCB.Checked = False
            End If
        End If


    End Sub



    Function ImageLay(imglay As String) As ImageLayout
        Select Case imglay
            Case "None"
                Return ImageLayout.None
            Case "Zoom"
                Return ImageLayout.Zoom
            Case "Stretch"
                Return ImageLayout.Stretch
        End Select

        Return ImageLayout.None

    End Function

    Public Sub Redraw()
        'PrevImgPB.BackgroundImage.Dispose()
        PrevImgPB.Image = _previmg
        Me.Refresh()
    End Sub

    Private Sub ZoomCB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ZoomCB.SelectedIndexChanged

        If sender.text = "None" Then
            PrevImgPB.Dock = DockStyle.None
            PrevImgPB.Size = New Size(_previmg.Width, _previmg.Height)
            PrevImgPB.SizeMode = PictureBoxSizeMode.Zoom
        ElseIf sender.text = "Zoom" Then
            'PrevImgPB.Size = New Size(My.Settings.PrevW, My.Settings.PrevH)
            PrevImgPB.SizeMode = PictureBoxSizeMode.Zoom
            PrevImgPB.Dock = DockStyle.Fill
        ElseIf sender.text = "Stretch" Then
            PrevImgPB.Dock = DockStyle.Fill
            PrevImgPB.SizeMode = PictureBoxSizeMode.StretchImage
        End If
        'PrevImgPB.SizeMode = ImageLay(ZoomCB.Text)
    End Sub


    Private Sub ImgR_Click(sender As Object, e As MouseEventArgs) Handles ImgR.Click, Panel2.MouseDown, PrevImgPB.Click
        If _picbox.Name = "FuzzyImagePB" Then Return

        If e.Button <> Windows.Forms.MouseButtons.Right And
            sender.name <> "ImgR" Then Return

        If e.Button = Windows.Forms.MouseButtons.XButton1 Then Me.Close()


        Debug.WriteLine("Img R click: " & _picbox.ToString)

        Dim PBIndex As Integer = ActivePBs.IndexOf(_picbox)
        Debug.WriteLine("Index: " & PBIndex.ToString)
        Dim FinalIndex = 0
        If PBIndex = ActivePBs.Count - 1 Then
            FinalIndex = 0
        Else
            FinalIndex = PBIndex + 1
        End If

        _picbox = ActivePBs(FinalIndex)
        Dim pcontrol As Control = _picbox.Parent
        Me.Text = pcontrol.Text

        PrevImgPB.Image = _picbox.Image
        _previmg = _picbox.Image

        If ZoomCB.Text = "None" And _previmg.Size <> PrevImgPB.Size Then

            PrevImgPB.Size = New Size(_previmg.Width, _previmg.Height)
            PrevImgPB.Dock = DockStyle.None
        Else
            PrevImgPB.Size = New Size(My.Settings.PrevW, My.Settings.PrevH)
            PrevImgPB.Dock = DockStyle.Fill
        End If


        TopTextLB.Text = _picbox.Tag

        Dim TTText As String = ""
        TTText += DimFromTopText(_picbox.Tag) & vbCr
        TTText += SizeFromTopText(_picbox.Tag) & vbCr
        TTText += FileFromTopText(_picbox.Tag) & vbCr
        With ImgDetsTT
            .ToolTipTitle = Me.Text
            .Show(TTText, Me, e.X + 10, e.Y + 10, My.Settings.MiniInfoT)
        End With

        'Me.Refresh()
    End Sub

    Function DimFromTopText(txt As String) As String
        Debug.WriteLine("Got: " & txt)
        Dim re2 As String = "(\(\d* x \d*\))"   'Round Braces 1

        Dim regex As Regex = New Regex(re2)
        Dim match As Match = regex.Match(txt)
        If match.Success Then
            Debug.WriteLine(match.Value)
            Return match.Value.ToString
        Else
            Return "(Size Unavailable)"
        End If
        Return "Error"
    End Function

    Function FileFromTopText(txt As String) As String
        Debug.WriteLine("Got: " & txt)
        Dim re2 As String = "(\):\s).*(\s\()"   'Round Braces 1

        Dim regex As Regex = New Regex(re2)
        Dim match As Match = regex.Match(txt)
        If match.Success Then
            Debug.WriteLine(match.Value.ToString.Trim({" "c, ")"c, ":"c, "("c}))
            Return match.Value.ToString.Trim({" "c, ")"c, ":"c, "("c})
        Else
            Return "(Size Unavailable)"
        End If
        Return "Error"
    End Function


    Function SizeFromTopText(txt As String) As String
        Debug.WriteLine("Got: " & txt)
        Dim re2 As String = "\) (\d* )KB$"   'Round Braces 1

        Dim regex As Regex = New Regex(re2)
        Dim match As Match = regex.Match(txt)
        If match.Success Then
            Debug.WriteLine(match.Value.ToString.Trim({" "c, ")"c, ":"c, "("c}))
            Return match.Value.ToString.Trim({" "c, ")"c, ":"c, "("c})
        Else
            Return "(Size Unavailable)"
        End If
        Return "Error"
    End Function

    Private Sub ImgL_Click(sender As Object, e As MouseEventArgs) Handles ImgL.Click, Panel2.MouseDown, PrevImgPB.MouseDown
        If _picbox.Name = "FuzzyImagePB" Then Return

        If e.Button <> Windows.Forms.MouseButtons.Left Then Return
        If e.Button = Windows.Forms.MouseButtons.XButton1 Then Me.Close()

        Debug.WriteLine("Img L click: " & _picbox.ToString)

        Dim PBIndex As Integer = ActivePBs.IndexOf(_picbox)
        Debug.WriteLine("Index: " & PBIndex.ToString)
        Dim FinalIndex = 0
        If PBIndex = 0 Then
            FinalIndex = ActivePBs.Count - 1
        Else
            FinalIndex = PBIndex - 1
        End If

        _picbox = ActivePBs(FinalIndex)
        Dim pcontrol As Control = _picbox.Parent
        Me.Text = pcontrol.Text

        PrevImgPB.Image = _picbox.Image
        _previmg = _picbox.Image

        If ZoomCB.Text = "None" And _previmg.Size <> PrevImgPB.Size Then
            PrevImgPB.Size = New Size(_previmg.Width, _previmg.Height)
            PrevImgPB.Dock = DockStyle.None
        Else
            PrevImgPB.Size = New Size(My.Settings.PrevW, My.Settings.PrevH)
            PrevImgPB.Dock = DockStyle.Fill
        End If

        TopTextLB.Text = _picbox.Tag

        Dim TTText As String = ""
        TTText += DimFromTopText(_picbox.Tag) & vbCr
        TTText += SizeFromTopText(_picbox.Tag) & vbCr
        TTText += FileFromTopText(_picbox.Tag) & vbCr
        With ImgDetsTT
            .ToolTipTitle = Me.Text

            .Show(TTText, Me, e.X + 10, e.Y + 10, My.Settings.MiniInfoT)
        End With

        ' Me.Refresh()
    End Sub


    Private Sub SeldCB_CheckedChanged(sender As Object, e As EventArgs) Handles SeldCB.CheckedChanged
        Debug.WriteLine("HERE: " & GameSetMatch.TopSelectedRow)
        Dim selrow() As DataRow = GameSetMatch._MainDT.Select("GID = " & GameSetMatch.TopSelectedRow)
        selrow(0).Item("Sel") = SeldCB.Checked
    End Sub

    Public Function FormOnScreen(frm As Form) As Boolean
        'If frm.IsHandleCreated Then
        '    Throw New InvalidOperationException()
        'End If
        'If Not frm.Visible OrElse frm.WindowState = FormWindowState.Minimized Then
        '    Return False
        'End If
        If My.Settings.PrevWinCloseState = FormWindowState.Maximized Then Return True

        Return PointVisible(New Point(frm.Left, frm.Top)) AndAlso PointVisible(New Point(frm.Right, frm.Top)) AndAlso PointVisible(New Point(frm.Right, frm.Bottom)) AndAlso PointVisible(New Point(frm.Left, frm.Bottom))
    End Function

    Private Shared Function PointVisible(p As Point) As Boolean
        Dim scr = Screen.FromPoint(p)
        Return scr.Bounds.Contains(p)
    End Function


End Class